package www.wen.com.common.lib.base.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.dylanc.loadinghelper.LoadingHelper
import com.dylanc.loadinghelper.ViewType
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import www.wen.com.common.BR
import www.wen.com.common.R
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter
import www.wen.com.common.lib.util.KeyboardUtils


/**
 * @author WEN
 * @Description:
 * @date 2020/11/25 17:49
 */
abstract class BaseVMFragment<K : ViewDataBinding> : Fragment(), View.OnClickListener, LoadingHelper.OnReloadListener {

    private var mIsInit = false

    var mLoadingHelper: LoadingHelper? = null

    protected lateinit var dataBinding: K

    protected lateinit var mRootView: View

    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?,
    ): View? {

        dataBinding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false)

        dataBinding.lifecycleOwner = this
        dataBinding.setVariable(BR.listener, this)

        //是否启用快捷标题栏
        isUseLoadingHelper()

        mRootView = mLoadingHelper?.decorView ?: dataBinding.root

        return mRootView
    }

    private fun isUseLoadingHelper() {
        if (isEnableLoadingHelper()) {
            mLoadingHelper = getLoadingHelper().apply {
                configurationLoadingHelper(this)
                setOnReloadListener(this@BaseVMFragment)
            }
        }
    }

    override fun onReload() {}

    open fun getLoadingHelper(): LoadingHelper {
        return LoadingHelper(dataBinding.root)
    }

    open fun configurationLoadingHelper(loadingHelper: LoadingHelper): LoadingHelper {
        loadingHelper.register(ViewType.TITLE, getTitleBarUniversalAdapter())
        loadingHelper.setDecorHeader(ViewType.TITLE)
        return loadingHelper
    }

    open fun getTitleBarUniversalAdapter(): LoadingHelper.Adapter<*> = TitleBarUniversalAdapter(requireActivity(), getString(R.string.logging_in), action = ::titleAction)

    open fun isEnableLoadingHelper() = false

    open fun titleAction(v: View) {
        when (v.id) {
            R.id.tvRight,
            R.id.ivRight,
            -> {
                actionRight()
            }
            R.id.tvLeft,
            R.id.ivLeft,
            -> {
                actionLeft()
            }
        }
    }

    open fun actionRight() {}

    open fun actionLeft() {
        findNavController().navigateUp()
    }

    override fun onResume() {
        super.onResume()
        if (!mIsInit) {
            lazyInitView()
            mIsInit = true
        } else {
            onResumeIsFirst()
        }
    }

    open fun onResumeIsFirst() {}

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
    }

    abstract fun lazyInitView()

    abstract fun getLayoutId(): Int

    override fun onDestroy() {
        super.onDestroy()
        KeyboardUtils.hideSoftInput(requireActivity())
    }

    override fun onClick(v: View) {}

    /**
     * 接收系统相册、相机的图片回调
     */
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            PictureConfig.CHOOSE_REQUEST -> {
                PictureSelector.obtainMultipleResult(data)
            }
        }
    }

}